import $api from './request.js'
export default {
	navTo(url){
		if(!uni.getStorageSync('token')){
			this.getInfo(url,'navTo');
			return
		}
		uni.navigateTo({
			url:`/pages/${url}`,
			complete(nav) {
				// console.log(nav.errMsg)
			}
		})
	},
	redTo(url){
		if(!uni.getStorageSync('token')){
			this.getInfo(url,'redTo');
			return
		}
		uni.redirectTo({
			url:`/pages/${url}`,
			complete(nav) {
				// console.log(nav.errMsg)
			}
		})
	},
	navBack(){
		uni.navigateBack()
	},
	showToast(title){
		uni.showToast({
			icon:'none',
			title:title
		})
	},
	teSplice(tel){
		tel = "" + tel;
		var ary = tel.split("");
		ary.splice(3,4,"****");
		var tel1=ary.join("");
		return tel1
	},
	getInfo(url,style){
		let that = this;
		wx.getUserProfile({
			desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			success: (res) => {
				wx.login({
					success:async (e)=>{
						uni.showLoading()
						let result = await $api.request($api.user.getUserByCode,{
							code: e.code,
							nickName: res.userInfo.nickName,
							photo: res.userInfo.avatarUrl
						})
						if(result){
							// console.log(result)
							if(result.telephone == ''){
								uni.setStorageSync('login_result',JSON.stringify(result));
								uni.navigateTo({
									url:'/pages/login/login'
								})
							}else{
								uni.setStorageSync('token',result.accessToken)
								that.getUserInfo(url,style);
							}
							uni.hideLoading();
						}
					}
				})
			}
		})
	},
	async getUserInfo(url,style) {
		let res = await $api.request($api.user.getinfo, {
			token: uni.getStorageSync('token')
		});
		if (res) {
			uni.setStorageSync('userInfo', res)
			this[`${style}`](url);
			uni.hideLoading()
		}
	}
}